#include "addfrienddialog.h"
#include <QPushButton>
#include <QScrollArea>
#include <QScrollBar>
#include <QLabel>

#include "model/datacenter.h"
#include "debug.h"

using namespace model;

//好友的一个搜索结果
FriendResultItem::FriendResultItem(const UserInfo& userInfo)
    :userInfo(userInfo)
{
    //设置基本属性
    this->setFixedHeight(70);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    //创建布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setVerticalSpacing(0);
    layout->setHorizontalSpacing(10);
    layout->setContentsMargins(0,0,20,0);
    this->setLayout(layout);

    //创建头像
    QPushButton* avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(50,50);
    avatarBtn->setIconSize(QSize(50,50));
    avatarBtn->setIcon(userInfo.avatar);

    //创建昵称
    QLabel* nameLabel = new QLabel();
    nameLabel->setFixedHeight(35); // 整个Item 70，昵称和个性签名各35
    nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    nameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    nameLabel->setStyleSheet("QLabel { font-size: 16px; font-weight: 700; }");
    nameLabel->setText(userInfo.nickname);

    // 创建个性签名
    QLabel* descLabel = new QLabel();
    descLabel->setFixedHeight(35);
    descLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    descLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    descLabel->setStyleSheet("QLabel { font-size: 14px; }");
    descLabel->setText(userInfo.description);

    // 创建添加好友按钮
    addBtn = new QPushButton();
    addBtn->setFixedSize(100, 40);
    addBtn->setText("添加好友");
    QString btnStyle = "QPushButton { border: none; background-color: rgb(137, 217, 97); color: rgb(255, 255, 255); border-radius: 10px;} ";
    btnStyle += "QPushButton:pressed { background-color: rgb(200, 200, 200); }";
    addBtn->setStyleSheet(btnStyle);

    // 7. 把上述内容, 添加到布局管理器中
    layout->addWidget(avatarBtn, 0, 0, 2, 1);
    layout->addWidget(nameLabel, 0, 1);
    layout->addWidget(descLabel, 1, 1);
    layout->addWidget(addBtn, 0, 2, 2, 1);

    connect(addBtn, &QPushButton::clicked, this, &FriendResultItem::clikeAddBtn);
}

//点击添加按钮触发的逻辑
void FriendResultItem::clikeAddBtn()
{
    DataCenter* dataCenter = DataCenter::getInstance();
    dataCenter->addFriendApplyAsync(userInfo.userId);

    addBtn->setEnabled(false);
    addBtn->setText("已申请");
    addBtn->setStyleSheet("QPushButton { border:none; color: rgb(255, 255, 255); background-color: rgb(200, 200, 200); border-radius: 10px;}");
}

/*
 * 添加好友的功能  搜索好友的窗口
 *
 */
AddFriendDialog::AddFriendDialog(QWidget* parent) :QDialog(parent)
{
    //设置基本属性
    this->setFixedSize(500, 500);
    this->setWindowTitle("添加好友");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QDialog { background-color: rgb(255,255,255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    //添加布局管理器
    layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(20,20,20,0);
    this->setLayout(layout);

    //创建搜索框
    searchEdit = new QLineEdit();
    searchEdit->setFixedHeight(50);
    searchEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    QString style = "QLineEdit { border: none; border-radius: 10px font-size: 16px; background-color: rgb(240,240,240); padding-left: 5px;  }";
    searchEdit->setStyleSheet(style);
    searchEdit->setPlaceholderText("按手机号/昵称搜索");
    layout->addWidget(searchEdit,0,0,1,8);

    //创建搜索按钮
    QPushButton* searchBtn = new QPushButton();
    searchBtn->setFixedSize(50,50);
    searchBtn->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    searchBtn->setIconSize(QSize(30, 30));
    searchBtn->setIcon(QIcon(":/resource/image/search_icon.png"));
    QString btnStyle = "QPushButton { border: none; background-color: rgb(240, 240, 240); border-radius: 10px; }";
    btnStyle +=  "QPushButton:hover { background-color: rgb(220,220,220);  } QPushButton:pressed { background-color: rgb(200,200,200); }";
    searchBtn->setStyleSheet(btnStyle);
    layout->addWidget(searchBtn,0,8,1,1);

    //初始化滚动区
    initResultArea();

#if TEST_UI
    QIcon avatar(":/resource/image/avatarDefault.png");
    for(int i=0; i<30; i++) {
        UserInfo* userInfo = new UserInfo();
        userInfo->userId = QString::number(i);
        userInfo->avatar = avatar;
        userInfo->nickname = "不摇碧莲" + QString::number(i);
        userInfo->description = "这是个性签名";
        this->addResult(*userInfo);
    }
#endif

    connect(searchBtn, &QPushButton::clicked, this, &AddFriendDialog::clickSearchBtn);
}

// 初始化结果显示区
void AddFriendDialog::initResultArea()
{
    //创建滚动区对象
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    scrollArea->setWidgetResizable(true);
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal {height: 0;} ");
    scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 2px; background-color: rgb(255,25,25); }");
    scrollArea->setStyleSheet("QScrollArea { border: none; }");
    //在1行0列 占据1行9列
    layout->addWidget(scrollArea,1,0,1,9);

    //创建QWidget
    resultContainer = new QWidget();
    resultContainer->setObjectName("#resultContainer");
    resultContainer->setStyleSheet("#resultContainer { background-color: rgb(255,255,255); }");
    scrollArea->setWidget(resultContainer);

    //向QWidget resultContainer中添加元素
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(0,0,0,0);
    resultContainer->setLayout(vlayout);
}

// 往窗口中新增一个好友搜索结果
void AddFriendDialog::addResult(const UserInfo& userInfo)
{
    FriendResultItem* item = new FriendResultItem(userInfo);
    resultContainer->layout()->addWidget(item);
}

//清空好友
void AddFriendDialog::clear()
{
    QVBoxLayout* layout = new QVBoxLayout();
    for(int i =layout->count()-1; i>=0; i--) {
        QLayoutItem* layoutItem = layout->takeAt(i);
        if(layoutItem == nullptr || layoutItem->widget() == nullptr) {
            continue;
        }
        //删除好友
        delete layoutItem->widget();
    }
}

//点击搜索按钮,出现对应的好友
void AddFriendDialog::clickSearchBtn()
{
    // 获取到对话框的内容
    const QString& text = searchEdit->text();
    if (text.isEmpty()) {
        return;
    }

    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter, &DataCenter::searchUserDone, this, &AddFriendDialog::clickSearchBtnDone, Qt::UniqueConnection);
    dataCenter->searchUserAsync(text);
}

//在搜索框中搜索
void AddFriendDialog::clickSearchBtnDone()
{
    DataCenter* dataCenter = DataCenter::getInstance();
    QList<UserInfo>* searchResult = dataCenter->getSearchUserResult();

    if(searchResult == nullptr) {
        return;
    }

    this->clear();
    for(const auto& u : *searchResult) {
        this->addResult(u);
    }
}

//搜索的关键字
void AddFriendDialog::setSearchKey(const QString& searchKey)
{
    searchEdit->setText(searchKey);
}


